package com.gateon.cg.creator.impl;


import com.gateon.cg.bean.Conf;
import com.gateon.cg.bean.TableInfo;
import com.gateon.cg.creator.AbstractFileCreator;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * swagger文件
 *
 * @author zhangjh
 */
public class ReqCalssCreator extends AbstractFileCreator {
    private static ReqCalssCreator creator;

    private ReqCalssCreator() {
        super();
    }

    private ReqCalssCreator(Conf conf) {
        super();
        init(conf);
    }

    public static synchronized ReqCalssCreator getInstance(Conf conf) {
        if (null == creator) {
            creator = new ReqCalssCreator(conf);
        }
        return creator;
    }

    @Override
    public void createFile(TableInfo tableInfo) throws IOException, TemplateException {
        Map<String,String> keys=new HashMap<>();
        keys.put("req.ftl","ListReq.java");
        keys.put("addreq.ftl","AddReq.java");
        keys.put("updatereq.ftl","UpdateReq.java");

        for (String key:keys.keySet()){
            String val = keys.get(key);
            String ftl = key;
            String fileName = tableInfo.getBeanName() + keys.get(key);
            String selfPath = conf.getReq_package();
            conf.setOptMark(val.split("\\.")[0]);
            System.out.println(selfPath);
            String prefixName = tableInfo.getBeanName().substring(0, 3).toLowerCase();
            Map<String, Object> root = new HashMap<String, Object>();
            root.put("table", tableInfo);
            root.put("conf", conf);
//            if (conf.isPrefix()) {//有表名类别
//                root.put("prefixName", prefixName);
//            }
            Template temp = cfg.getTemplate(ftl);
            System.out.println(temp.getName());
            fileName = javaPath + selfPath  + separator + fileName;
            createFile(fileName, root, temp);
        }


    }

}
